package com.banyuan.HomeWork.集合类.数据结构;

public class MyDoubleLinkedList {
    private DNote first;
    private DNote last;
    private int size;

    //在队列的最前端添加一个元素；
    public void addFirst(Object obj) {
        DNote dNote = new DNote();
        dNote.setObj(obj);
        dNote.setNext(first);
        first = dNote;
        size++;
        if (size == 1) {
            last = first;
        }
    }

    //在队列的最后端添加一个元素；
    public void addLast(Object obj) {
        DNote dNote = new DNote();
        dNote.setObj(obj);
        dNote.setPre(last);
        last = dNote;
        size++;
        if (size == 1) {
            first = last;
        }
    }

    //删除队列的首个元素；
    public void removeFirst() {
        if (size == 0) {
            throw new NullPointerException();
        } else if (size == 1) {
            first = null;
            last = null;
        } else {
            first = first.getNext();
            first.setPre(null);
        }
        size--;

    }

    //删除队列的最后一个元素；
    public void removeLast() {
        if (size == 0) {
            throw new NullPointerException();
        } else if (size == 1) {
            first = null;
            last = null;
        } else {
            last = last.getPre();
            last.setNext(null);
        }
        size--;
    }

    //获得DoubleLinkedList中指定下标的节点；
    private DNote getDNode(int index) {
        if (first == null) {
            throw new NullPointerException();
        }
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
        DNote temp = new DNote();
        if (index < size >> 2) {
            temp = first;
            for (int i = 0; i < index; i++) {
                temp = temp.getNext();
            }
        } else if (index >= size >> 2) {
            temp = last;
            for (int i = size - 1; i > index; i++) {
                temp = temp.getPre();
            }
        }
        return temp;
    }

    //获取指定下标的元素；
    public Object get(int index) {
        return getDNode(index).getObj();
    }

    //向指定的下标处添加元素
    public void add(int index, Object obj) {
        if (first == null) {
            throw new NullPointerException();
        }
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
        if (index == 0) {
            addFirst(obj);
        } else if (index > 0) {
            DNote temp = new DNote();
            temp = getDNode(index);

        }
    }
}

//双向列表节点有两个位置用于保存；
//前位置保存之前的一个节点的地址值，首节点为null；
//后位置保存后一个节点的地址值，尾节点为null；
class DNote {
    private Object obj;
    private DNote next;
    private DNote pre;

    public DNote() {
    }

    public DNote(Object obj, DNote next, DNote pre) {
        this.obj = obj;
        this.next = next;
        this.pre = pre;
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }

    public DNote getNext() {
        return next;
    }

    public void setNext(DNote next) {
        this.next = next;
    }

    public DNote getPre() {
        return pre;
    }

    public void setPre(DNote pre) {
        this.pre = pre;
    }

    @Override
    public String toString() {
        return obj.toString();
    }
}
